<?php namespace Controllers;
use Libraries;
class Kavling extends Libraries\PController {
    private $id, $nama, $id_proyek, $status;
    public function __construct() {
        parent::__construct();
        $this->db = new Libraries\Mysql;
    }
    public function index() {
        if (Libraries\u::isAjaxRequest()) {
            $kavling = $this->db->results("SELECT * FROM psn_kavling");
            $this->outputJSON(array('success' => 'true', 'results' => $kavling));
        } else {
            $this->redirect('home');
        }
    }
    public function create() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $this->db->results("INSERT INTO psn_kavling
	           (nama, id_proyek, status) VALUES ('$this->nama', $this->id_proyek, $this->status );");
            $id = $this->db->insertId();
            $msg = "Data berhasil di simpan dengan id $id";
            $this->outputJSON(array('success' => true, 'msg' => $msg));
        } else {
            $this->redirect('home');
        }
    }
    public function update() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $msg = "Data dengan id $this->id berhasil di simpan.";
            $this->db->results("UPDATE psn_kavling SET nama = '$this->nama', id_proyek = '$this->id_proyek',
	           status = $this->status WHERE id_kavling = $this->id;");
            $this->outputJSON(array('success' => true, 'msg' => $msg));
        } else {
            $this->redirect('home');
        }
    }
    private function get_submit() {
        foreach ($_POST as $key => $val){
            $_POST[$key] = $this->request->post($key, FILTER_SANITIZE_MAGIC_QUOTES);
            $_POST[$key] = $this->db->escape($_POST[$key]);
            $this->$key = $val;
        }
    }
}
